<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>广告管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>


	<!-- 富文本编辑器 -->
	<link rel="stylesheet" href="../plugins/kindeditor/themes/default/default.css" />
	<script charset="utf-8" src="../plugins/kindeditor/kindeditor-min.js"></script>
	<script charset="utf-8" src="../plugins/kindeditor/lang/zh_CN.js"></script>


	<!-- bootstarp-fileinput -->
	<link rel="stylesheet" type="text/css" href="../js/bootstrap-fileinput/css/fileinput.min.css"/>
	<script type="text/javascript" src="../js/bootstrap-fileinput/js/fileinput.min.js"></script>
	<script type="text/javascript" src="../js/bootstrap-fileinput/js/locales/zh.js"></script>



</head>

<body class="hold-transition skin-red sidebar-mini">
  <!-- .box-body -->


  	<script>

		$(function () {
			fileInit();
		})

		//初始化上传插件 及上传路径和回显配置
		function fileInit(){
			$('#myFile').fileinput({
				language: 'zh',
				uploadUrl: '/tbContentController/uploadImage',
				maxFileCount :3, // multiple
				enctype : 'multipart/form-data',
				maxFileSize : 1024, //单位为kb
				allowedFileExtensions : ['jpg', 'gif', 'png'],//限制上传文件类型
				browseClass: 'btn btn-primary'
			});

			$('#myFile').on('fileuploaded',function(event, data, previewId, index){
				//data.response获取的就是后台return 的对象
				var result = data.response;
				var url = result.data;
				$("#fileAddr").val(url);
			})

			$('#myFiles').fileinput({
				language: 'zh',
				uploadUrl: '/tbContentController/uploadImages',
				maxFileCount :3, // multiple
				enctype : 'multipart/form-data',
				maxFileSize : 1024, //单位为kb
				allowedFileExtensions : ['jpg', 'gif', 'png'],//限制上传文件类型
				browseClass: 'btn btn-primary'
			});

			$('#myFiles').on('fileuploaded',function(event, data, previewId, index){
				//data.response获取的就是后台return 的对象
				var result = data.response;
				var url = result.data;
				$("[name=fileAddrs]").val(url);
			})

		}


		function addTbContent() {
			var status;
			var boxs=$("input[name='checks']:checked").val();
			if(boxs==1){
				status=boxs;
			}else {
				status=0;
			}

			$.ajax({
				url:"/tbContentController/addTbContent",
				type:"get",
				datatype:"json",
				data:{status:status,categoryId:$("[name=categoryId]").val(),title:$("[name=title]").val(),url:$("[name=url]").val(),pic:$("[name=rimage]").val(),sortOrder:$("[name=sortOrder]").val()},
				async:true,
				success:function(result){
					alert(result.member+","+result.content+","+JSON.stringify(result.data));
				},
				error:function(result){
					alert("系统异常，请联系管理员！！！");
				}
			})
		}


		function doUpdate(obj){
			$.ajax({
				url:"/tbContentController/doUpdate",
				type:"get",
				datatype:"json",
				data:{id:obj},
				async:true,
				success:function(result){
					var se=$("#categoryId").find("option");
					for (var x=0;x<se.length;x++){
						if ($(se[x]).val()==result.categoryId){
							$(se[x]).attr("selected","selected");
						}
					}
					$("#title").val(result.title);
					$("#idd").val(result.id);
					$("#url").val(result.url);
					$("#sortOrder").val(result.sortOrder);
					$("[name=fileAddr]").val(result.pic);
					$("#yuan").attr("src",result.pic);
					if(result.status==1){
						$("#checks").prop("checked",true);
					}
				},
				error:function(result){
					alert("系统异常，请联系管理员！！！");
				}
			})
		}



		function deleteTbContent(){
			var boxs=document.getElementsByName("box");
			var ids="";
			for (var i = 0; i < boxs.length; i++) {
				if(boxs[i].checked){
					ids+=",'"+boxs[i].value+"'";
				}
			}
			ids=ids.substring(1);
			$.ajax({
				url:"/tbContentController/deleteTbContent",
				type:"get",
				datatype:"json",
				data:{ids:ids},
				async:true,
				success:function(result){
					alert(result.member+","+result.content+","+JSON.stringify(result.data));
				},
				error:function(result){
					alert("系统异常，请联系管理员！！！");
				}
			})
		}


		function updateTbContent() {
			var status;
			var boxs=$("input[id='checks']:checked").val();
			if(boxs==1){
				status=boxs;
			}else {
				status=0;
			}

			$.ajax({
				url:"/tbContentController/updateTbContent",
				type:"get",
				datatype:"json",
				data:{id:$("#idd").val(),status:status,categoryId:$("#categoryId").val(),title:$("#title").val(),url:$("#url").val(),pic:$("[name=fileAddrs]").val(),sortOrder:$("#sortOrder").val()},
				async:true,
				success:function(result){
					alert(result.member+","+result.content+","+JSON.stringify(result.data));
				},
				error:function(result){
					alert("系统异常，请联系管理员！！！");
				}
			})
		}

	</script>
                
                    <div class="box-header with-border">
                        <h3 class="box-title">广告管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" onclick="deleteTbContent()" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="开启" onclick='confirm("你确认要开启吗？")'><i class="fa fa-check"></i> 开启</button>
                                        <button type="button" class="btn btn-default" title="屏蔽" onclick='confirm("你确认要屏蔽吗？")'><i class="fa fa-ban"></i> 屏蔽</button>
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                                  
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">广告ID</th>
									      <th class="sorting">分类ID</th>
									      <th class="sorting">标题</th>
									      <th class="sorting">URL</th>		
									      <th class="sorting">图片</th>	
									      <th class="sorting">排序</th>		
									      <th class="sorting">是否有效</th>											     						      							
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody>
								  <th:block th:each="tc:${tcs}">
									  <tr>
										  <td><input th:value="${tc.id}" name="box" type="checkbox"></td>
										  <td th:text="${tc.id}"></td>
										  <td th:text="${tc.categoryId}"></td>
										  <td th:text="${tc.title}"></td>
										  <td th:text="${tc.url}"></td>
										  <td>
											  <img alt="" th:src="${tc.pic}" width="100px" height="50px">
										  </td>
										  <td th:text="${tc.sortOrder}"></td>
										  <td th:if="${tc.status=='1'}">有效</td>
										  <td th:if="${tc.status=='0'}">无效</td>

										  <td class="text-center">
											  <button type="button" th:value="${tc.id}" onclick="doUpdate(this.value)" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#updateModal">修改</button>
										  </td>
									  </tr>
								  </th:block>
			                      </tbody>
			                  </table>
			                  <!--数据列表/--> 
                        </div>
                        <!-- 数据表格 /-->
                     </div>
                    <!-- /.box-body -->


  <!-- 编辑窗口 -->
  <div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel">广告增加</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td>广告分类</td>
						  <td>
							  <select class="form-control" name="categoryId">
								  <option value="-1">请选择</option>
								  <option th:each="tg:${tgs}" th:value="${tg.id}" th:text="${tg.name}"></option>
							  </select>
						  </td>
					  </tr>
					  <tr>
						  <td>标题</td>
						  <td><input name="title" class="form-control" placeholder="标题" >  </td>
					  </tr>
					  <tr>
						  <td>URL</td>
						  <td><input name="url" class="form-control" placeholder="URL" >  </td>
					  </tr>
					  <tr>
						  <td>排序</td>
						  <td><input name="sortOrder" class="form-control" placeholder="排序" >  </td>
					  </tr>
					  <tr>
						  <td>广告图片</td>
						  <td>
							  <table>
								  <tr>
									  <td>
										  <label class="control-label" for="myFile">上传图片</label><!--multiple 支持多文件上传  不加这个只能上传一个文件  -->
										  <input type="file" id="myFile" name="myFile" multiple />
										  <input type="text" name="rimage" id="fileAddr"  value="">
										  <input type="hidden"  name="sourceid" value="0"/>
									  </td>
								  </tr>
							  </table>
						  </td>
					  </tr>
					  <tr>
						  <td>是否有效</td>
						  <td>
							  <input type="checkbox" value="1" name="checks" class="icheckbox_square-blue" >
						  </td>
					  </tr>
				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" onclick="addTbContent()" aria-hidden="true">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>

  <!-- 编辑窗口 -->
  <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel1" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel1">广告修改</h3>
			  </div>
			  <div class="modal-body">

				  <input id="idd" type="hidden">
				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td>广告分类</td>
						  <td>
							  <select class="form-control" id="categoryId">
								  <option value="-1">请选择</option>
								  <option th:each="tg:${tgs}" th:value="${tg.id}" th:text="${tg.name}"></option>
							  </select>
						  </td>
					  </tr>
					  <tr>
						  <td>标题</td>
						  <td><input id="title" class="form-control" placeholder="标题" >  </td>
					  </tr>
					  <tr>
						  <td>URL</td>
						  <td><input id="url" class="form-control" placeholder="URL" >  </td>
					  </tr>
					  <tr>
						  <td>排序</td>
						  <td><input id="sortOrder" class="form-control" placeholder="排序" >  </td>
					  </tr>
					  <tr>
						  <td>广告图片</td>
						  <td>

							  </small><img id="yuan" src="" width="90px" height="45px">
						  </td>
						  <td>
							  <table>
								  <tr>
									  <td>
										  <label class="control-label" for="myFile">上传图片</label><!--multiple 支持多文件上传  不加这个只能上传一个文件  -->
										  <input type="file" id="myFiles" name="myFiles" multiple />
										  <input type="text" name="fileAddrs" value="">
										  <input type="hidden" id="" name="sourceid" value="0"/>
									  </td>
								  </tr>
							  </table>
						  </td>
					  </tr>
					  <tr>
						  <td>是否有效</td>
						  <td>
							  <input type="checkbox" value="1" id="checks" class="icheckbox_square-blue" >
						  </td>
					  </tr>
				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" onclick="updateTbContent()" aria-hidden="true">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
</body>

</html>